<template>
  <div class="login-container">
    <div class="login_box">
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <div class="yonghu">
          <img class="logo" src="../../assets/xiazai.png" alt />
          <span>用户登录</span>
        </div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入用密码"
            show-password
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-button class="denglu" type="primary" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  data() {
    return {
      // 这是登陆表单的数据绑定对象
      loginForm: {
        username: 'root@admin.com',
        password:
          '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
      },
      // 这是表单验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登陆密码', trigger: 'blur' }
          // { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async login() {
      // 请求登陆
      try {
        const { data } = await login(this.loginForm)
        // console.log(data)
        this.$message.success('登陆成功')
        window.sessionStorage.setItem('token', data.token)
        this.$router.push('/dashboard')
      } catch (err) {
        // console.log('登陆失败,请重试')
        this.$message.error('登陆失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: url(~@/assets/mian.jpg);
  background-color: #497d93;
  height: 100%;
  width: 100%;
  background-size: 100%;
  background-repeat: no-repeat;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #f5f5f5;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .yonghu {
    font-size: 22px;
    padding-bottom: 15px;
    span {
      padding-left: 8px;
    }
    .logo {
      position: relative;
      bottom: -5px;
    }
  }
  .login_form {
    position: absolute;
    bottom: 60px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .denglu {
    width: 100%;
  }
}
</style>
